<?php
// @formatter:off
/**
 * @file c.random.php
 * @author Alejandro Dario Simi
 * @date $Date: 2013-06-16 23:56:49 +0000 (Sun, 16 Jun 2013) $
 *
 * $Id: c.random.php 73 2013-06-16 23:56:49Z daemonraco@gmail.com $
 * $URL: http://wcomix.googlecode.com/svn/tags/wcomix-1.0-BETA1/themes/default/controlers/contents/c.random.php $
 */
// @formatter:on

if($wcProfile->allowedTo(WC_PERM_SPECIAL_RANDOM)) {
	require_once "{$this->_themeRoot}/controlers/generics/bundlepreview.php";
	require_once "{$this->_themeRoot}/controlers/generics/comicpreview.php";

	global $wcProfile;
	global $wcReadRate;
	global $wcGroup;
	global $wcGroupForbidden;
	global $wcDefaults;
	global $wcThemeAssigns;
	global $wcUser;
	global $wcDatabaseAccess;
	global $wcTagsHolder;
	global $wcDirectoriesHolder;
	global $wcComicsHolder;

	$wcThemeAssigns["PATH"] = "Randoms";
	$wcThemeAssigns['ACTIONCONTENT'] = "/contents/c.random.html";

	$ids = 1;
	$limit = 20;
	$stmtId = basename(__FILE__);

	$db = WCConnection::Instance();
	$result = false;
	$stmt = false;
	if($wcUser) {
		$query = "select  *\n";
		$query .= "from    {$wcDatabaseAccess['prefix']}comics\n";
		$query .= "where   cmc_group = :group\n";
		$query .= " and    cmc_lost  = 'N'\n";
		$query .= " and    cmc_id not in (\n";
		$query .= "                select  crr_comic\n";
		$query .= "                from    {$wcDatabaseAccess['prefix']}read_rates\n";
		$query .= "                where   crr_group = :group\n";
		$query .= "                 and    crr_user  = :user\n";
		$query .= "                 and    crr_read  = 'Y'\n";
		$query .= "        )\n";
		$query .= "order by rand()\n";
		$query .= "limit {$limit}";
		$stmt = $db->prepare("{$stmtId}[1]", $query);

		$params = array(
			":group" => $wcGroup->id,
			":user" => $wcUser->id()
		);
		$stmt->execute($params);
	}

	if(!$stmt || ($stmt !== false && $stmt->rowCount() < $limit)) {
		$query = "select  *\n";
		$query .= "from    {$wcDatabaseAccess['prefix']}comics\n";
		$query .= "where   cmc_group = :group\n";
		$query .= " and    cmc_lost  = 'N'\n";
		$query .= "order by rand()\n";
		$query .= "limit {$limit}";
		$stmt = $db->prepare("{$stmtId}[2]", $query);

		$params = array(":group" => $wcGroup->id);
		$stmt->execute($params);
	}

	$error = $stmt->errorInfo();
	if(!$error[1]) {
		$comics = array();
		foreach($stmt->fetchAll() as $comicData) {
			$comic = $wcComicsHolder->item($comicData["cmc_id"], $wcGroup);
			if($comic && $comic->ok()) {
				$comics[] = wcGetComicPreviewAssigns($comic);
			}
			unset($comic);
		}

		$wcThemeAssigns["comics"] = $comics;
	}

	//
	// Random Tags
	$limit = 5;
	$having = 50;

	$query = "select  *\n";
	$query .= "from    {$wcDatabaseAccess['prefix']}tags\n";
	$query .= "where   tag_group = :group\n";
	$query .= " and    tag_count > :having\n";
	$query .= "order by rand()\n";
	$query .= "limit {$limit}";
	$stmtT = $db->prepare("{$stmtId}[4]", $query);

	$params = array(
		":group" => $wcGroup->id,
		":having" => $having
	);

	$stmtT->execute($params);
	while($stmtT->rowCount() < $limit && $params[":having"] > 0) {
		$params[":having"] -= 10;
		if($params[":having"] < 0) {
			$params[":having"] = 0;
		}
		$stmtT->execute($params);
	}

	if($stmtT->rowCount() > 0) {
		$tags = array();
		foreach($stmtT->fetchAll() as $tagData) {
			$tag = $wcTagsHolder->item($tagData["tag_id"], $wcGroup);
			if($tag && $tag->ok()) {
				$tags[$tag->name] = wcGetTagPreviewAssigns($tag);
			}
		}
		ksort($tags);

		$wcThemeAssigns["tags"] = $tags;
	}

	//
	// Random Directories
	$limit = 5;
	$having = 50;

	$query = "select  *\n";
	$query .= "from    {$wcDatabaseAccess['prefix']}directories\n";
	$query .= "where   dir_group = :group\n";
	$query .= " and    dir_count > :having\n";
	$query .= "order by rand()\n";
	$query .= "limit {$limit}";
	$stmtD = $db->prepare("{$stmtId}[3]", $query);

	$params = array(
		":group" => $wcGroup->id,
		":having" => $having
	);

	$stmtD->execute($params);
	while($stmtD->rowCount() < $limit && $params[":having"] > 0) {
		$params[":having"] -= 10;
		if($params[":having"] < 0) {
			$params[":having"] = 0;
		}
		$stmtD->execute($params);
	}

	if($stmtD->rowCount() > 0) {
		$directories = array();
		foreach($stmtD->fetchAll() as $directoryData) {
			$directory = $wcDirectoriesHolder->item($directoryData["dir_id"], $wcGroup);
			if($directory && $directory->ok()) {
				$directories[$directory->name] = wcGetDirectoryPreviewAssigns($directory);
			}
		}
		ksort($directories);

		$wcThemeAssigns["directories"] = $directories;
	}
} else {
	$wcThemeAssigns["error-string"] = "You're not allowed to visit the random items page";
	require "{$this->_themeRoot}/controlers/contents/c.403.php";
}
?>